<?php 
namespace app\admin\controller;

use app\common\model\cash\CashRecharge;
use app\common\model\user\Users;
use think\Db;
use app\common\controller\Log;
use think\Exception;
use app\common\model\coin\CoinUser;
use app\common\model\cash\CashList;
use app\common\model\coin\CoinList;

class Finance extends Base
{
	/**
	 * 充币记录
	 * @return unknown
	 */
	public function rechargeList()
	{
		$data = $this->request->post(''); //获取客户端传递的数据
		$where = []; //初始化查询条件
		//重组查询数据
		if(!empty($data['user_name'])){
			$where['user_id'] = Users::where(['user_name' => $data['user_name']])->value('id');
		}
		if(!empty($data['coin_id']))$where['coin_id'] = $data['coin_id'];
		if(!empty($data['times'])) {
			$start = strtotime($data['times'][0]);
			$end = strtotime($data['times'][1]);
			$where['create_time'] = ['between',[$start,$end]];
		}
		$list = CashRecharge::where($where)->order('id desc')->paginate();
		return jsonSuccess($list);
	}
	/**
	 * 获取提币、充币汇总
	 * @return unknown
	 */
	public function rechargeGather()
	{
		$data = $this->request->post(''); //获取客户端传递的数据
		$start_time = strtotime(date('Y-m-d').'00:00:00');
		$end_time = strtotime(date('Y-m-d').'23:59:59');
		$where = ['state' => 1]; //初始化查询条件
		$where_day = ['state' => 1,'create_time' => ['between',[$start_time,$end_time]]];
		//获取查询表1提币，0充币
		$style = $this->request->post('style');
		$cash = $style ? new CashList() : new CashRecharge();
		//获取币种列表
		$coin_list = CoinList::where(['state' => 1])->select();
		$info = [];
		if(!empty($data['user_name'])){
			$user_id = Users::where(['user_name' => $data['user_name']])->value('id');
			$where['user_id'] = $user_id;
			$where_day['user_id'] = $user_id;
		}
		if(!empty($data['times'])) {
			$start_time = strtotime($data['times'][0]);
			$end_time = strtotime($data['times'][1]);
			$where['create_time'] = ['between',[$start_time,$end_time]];
		}
		foreach ($coin_list as $rows) {
			$where['coin_id'] = $rows['id'];
			$where_day['coin_id'] = $rows['id'];
			$all_num = $cash->where($where)->sum('num');
			$day_num = $cash->where($where_day)->sum('num');
			$temp = [
					'coin_name' => $rows['name'],
					'all_num' => $all_num,
					'day_num' => $day_num
			];
			$info[] = $temp;
		}
		return jsonSuccess($info);
	}
	/**
	 * 拒绝充币
	 * @throws Exception
	 * @return unknown
	 */
	public function refuseRecharge()
	{
		$id = $this->request->param('id');
		Db::startTrans();
		try {
			$recharge = CashRecharge::get($id);
			if($recharge->state)throw new Exception('状态错误');
			$update = ['state' => 2 , 'update_time' => time()];
			$recharge->save($update);
			//添加日志
			Log::addLog(2, "拒绝用户{$recharge->user_id->user_name}充币申请");
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess('拒绝成功');
	}
	
	/**
	 * 同意充币
	 * @throws Exception
	 * @return unknown
	 */
	public function agreeRecharge()
	{
		$id = $this->request->post('id');
		Db::startTrans();
		try {
			//更改充币状态
			$recharge = CashRecharge::get($id);
			if($recharge->state)throw new Exception('状态错误');
			$update = ['state' => 1, 'update_time' => time()];
			$recharge->save($update);
			//给用户加钱
			CoinUser::where(['user_id' => $recharge->user_id->id , 'coin_id' => $recharge->coin_id->id])->setInc('avalible_price',$recharge->num);
			//添加日志
			Log::addLog(2, "同意用户{$recharge->user_id->user_name}充币申请");
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess('同意成功');
	}
	
	/**
	 * 提币列表
	 * @return unknown
	 */
	public function cashList()
	{
		$data = $this->request->post(''); //获取客户端传递的数据
		$where = []; //初始化查询条件
		//重组查询数据
		if(!empty($data['user_name'])){
			$where['user_id'] = Users::where(['user_name' => $data['user_name']])->value('id');
		}
		if(!empty($data['coin_id']))$where['coin_id'] = $data['coin_id'];
		if(!empty($data['times'])) {
			$start = strtotime($data['times'][0]);
			$end = strtotime($data['times'][1]);
			$where['create_time'] = ['between',[$start,$end]];
		}
		$list = CashList::where($where)->order('id desc')->paginate();
		return jsonSuccess($list);
	}
	
	/**
	 * 同意提币
	 * @throws Exception
	 * @return unknown
	 */
	public function agreeCash()
	{
		$id = $this->request->post('id');
		Db::startTrans();
		try {
			$cash = CashList::get($id);
			if($cash->state)throw new Exception('状态错误');
			//更改状态
			$update = ['state' => 1 , 'update_time' => time()];
			$cash->save($update);
			//扣除用户响应的冻结金额
			CoinUser::where(['user_id' => $cash->user_id->id , 'coin_id' => $cash->coin_id->id])->setDec('freezing_price',$cash->num);
			//添加日志
			Log::addLog(2, "同意用户{$cash->user_id->user_name}提币申请");
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess('同意成功');
	}
	
	/**
	 * 拒绝提币
	 * @throws Exception
	 * @return unknown
	 */
	public function refuseCash()
	{
		$id = $this->request->post('id');
		Db::startTrans();
		try {
			$cash = CashList::get($id);
			if($cash->state)throw new Exception('状态错误');
			//更改状态
			$update = ['state' => 2 , 'update_time' => time()];
			$cash->save($update);
			//扣除用户响应的冻结金额
			CoinUser::where(['user_id' => $cash->user_id->id , 'coin_id' => $cash->coin_id->id])->setDec('freezing_price',$cash->num);
			//增加用户响应的可用金额
			CoinUser::where(['user_id' => $cash->user_id->id , 'coin_id' => $cash->coin_id->id])->setInc('avalible_price',$cash->num);
			//添加日志
			Log::addLog(2, "拒绝用户{$cash->user_id->user_name}提币申请");
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess('拒绝成功');
	}
	
}